package com.charity.contoller;

import com.charity.model.CommonResponse;
import com.charity.pojo.Advertisement;
import com.charity.service.AdvertisementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
public class AdvertisementController {

    @Autowired
    private AdvertisementService advertisementService;

    private CommonResponse commonResponse = new CommonResponse();

    @PostMapping("/updateAdvertisement")
    private CommonResponse updateAdvertisement(@RequestBody Advertisement advertisement) {
        Integer result = advertisementService.updateAdvertisement(advertisement.getNo(), advertisement.getTitle(), advertisement.getContent());
        if (result > 0 ) {
            return commonResponse.ok(result);
        }
        return commonResponse.cuw("300", "更新失败");
    }

    @PostMapping("/addAdvertisement")
    private CommonResponse addAdvertisement(@RequestBody Advertisement advertisement) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Integer result = advertisementService.addAdvertisement(advertisement.getTitle(), advertisement.getContent(), sdf.format(new Date()));
        if (result > 0) {
            return commonResponse.ok(result);
        }
        return commonResponse.cuw("300", "添加失败");
    }

    @PostMapping("/deleteAdvertisement")
    private CommonResponse deleteAdvertisement(@RequestBody Advertisement advertisement) {
        Integer result = advertisementService.deleteAdvertisement(advertisement.getNo());
        if (result > 0) {
            return commonResponse.ok(result);
        }
        return commonResponse.cuw("300", "删除失败");
    }

}
